<?php
/**
 * Created By Project
 * Author: 陈东
 * Copyright：陈东
 * Source Code：陈东
 * Explain：I shall not authorize for any other commercial purposes, law breakers must be prosecuted.
 */
namespace Home\Controller;
use Think\Controller;
class ZchouController extends Controller {
	/*获取当前页面地址*/
	protected function get_url() {
		$sys_protocal = isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == '443' ? 'https://' : 'http://';
		$php_self = $_SERVER['PHP_SELF'] ? $_SERVER['PHP_SELF'] : $_SERVER['SCRIPT_NAME'];
		$path_info = isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : '';
		$relate_url = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : $php_self.(isset($_SERVER['QUERY_STRING']) ? '?'.$_SERVER['QUERY_STRING'] : $path_info);
		return $sys_protocal.(isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '').$relate_url;
	}
	
	public function index(){
		$m = M('info','pro_');
		$this->assign('pro',$m->where(array('pro_state'=>1))->select());
		$this->display();
	}

	public function indexdata(){
		$m = M('info','pro_');
		$res = $m->where(array('pro_state'=>1))->select();
		return $this->returnApiSuccess(1,$res);
	}
	
	protected function returnApiSuccess($msg = null,$data = array()){
		$result = array(
			'msg' => $msg,
			'data' =>$data
		);
		print json_encode($result);
	}
	
	public function prodeail($id=null){
		if(!empty($id)){
			$m = M('info','pro_');
			$result = $m->find($id);
			
			$record = M('record','pro_');
			$record_result = $record->where(array('pro_id'=>$id))->select();
			$people_count = count($record->distinct(true)->where(array('pro_id'=>$id))->field('remit_people')->select()); //计算人数
			
			$this->assign('moneynum',$this->moneysum($record_result));
			$this->assign('peoplecount',$people_count);
			$this->assign('proinfo',$result);
			$this->assign('geturl',urlencode($this->get_url()));
			$this->assign('datecha',strtotime(date('Y-m-d'))-strtotime($result['pro_endtime']));
			$this->assign('startdatecha',strtotime($result['pro_starttime'])-strtotime(date('Y-m-d')));
			
			
			/*筹款记录*/
			$record = M('record','pro_');
			$this->assign('record',$record->where(array('pro_id'=>$id))->order('remit_time desc')->select());
			$this->display();
		}else{
			echo "<script>alert('参数错误！');history.go(-1);</script>";
		}
	}
	
	public function prodeailinfo($id=null){
		if(!empty($id)){
			$m = M('info','pro_');
			$result = $m->find($id);
			$this->assign('proinfo',$result);
			$this->display();
		}else{
			echo "<script>alert('参数错误！');history.go(-1);</script>";
		}
	
	}
	
	/*
	 * 基金会简介
	 */
	public function jjhinfo($id=null){
		if(!empty($id)){
			$d = D(C('TABLE_JIJINHUI'));
			$result = $d->find($id);
			$this->assign('proinfo',$result);
			$this->display();
		}else{
			echo "<script>alert('参数错误！');history.go(-1);</script>";
		}
	
	}
	
	/*计算金额总和*/
	protected function moneysum($array){
		if(is_array($array)){
			$num = 0;
			foreach ($array as $v){
				$num += $v['remit_money'];
			}
			return $num;
		}else{
			return 0;
		}

	}
	
	/*
	 * 支付成功后插入记录
	 */
	public function cdata(){
		$lastinsertid = I('post.lastinsertid');
		$m = M('record','pro_');
		$res = $m->where(array('id'=>$lastinsertid))->delete();
		if($res){
			echo 1;
		}else{
			echo 0;
		}
	}
	
	/*
	 * 我捐款的记录
	 */
	public function mydonation(){
		if(!empty($this->islogin())){
			$cookieuserinfo = $this->islogin();
		}else{
			$cookieuserinfo = 0;
		}
		$result = M()->table(array('pro_record'=>'r','pro_info'=>'i'))->field('r.*,i.*')->where("r.pro_id=i.id and r.remit_people='".$cookieuserinfo."'")->select();
		$this->assign('donation',$result);
		$this->display();

	}
	
	/*
	 * 捐款排行
	 */
	public function paihang(){
		if(!empty($this->islogin())){
			$cookieuserinfo = $this->islogin();
		}
		$record = M('record','pro_');
		$p = $record->distinct(true)->field('remit_people')->select();
		$reultarray = array();
		foreach ($p as $v){
			$username = W('Wxuserinfo/getuserinfos',array('token'=>$v['remit_people'],'filed'=>'nickname'));
			$userimg = W('Wxuserinfo/getuserinfos',array('token'=>$v['remit_people'],'filed'=>'headimgurl'));
			$reultarray[] = $this->getmoneysumbypeople($v['remit_people'],$userimg,$username);
		}
		foreach ($reultarray as $key => $r){
			$remit_money[$key]  = $r['remit_money'];
		}
		array_multisort($remit_money, SORT_DESC, $reultarray);
		
		//计算我的排名位置并返回我的信息
		$i = 0;
		foreach ($reultarray as $p){
			$i++;
			if($p['remit_people']==$cookieuserinfo){
				$pm = $i;
				$myaray = $p;
				break;
			}
		}
		//我的排名
		$this->assign('myinfo',$myaray);
		$this->assign('mypm',$pm);
		//总排行
		$this->assign('record',$reultarray);
		$this->display();
	
	}
	
	protected function getmoneysumbypeople($people,$userimg,$username){
		$record = M('record','pro_');
		$result = $record->where(array('remit_people'=>$people))->field('remit_money')->select();
		$rarray = array();
		foreach ($result as $v){
			$sum += $v['remit_money'];
		}
		$rarray['userimg'] = $userimg;
		$rarray['username'] = $username;
		$rarray['remit_money'] = $sum;
		$rarray['remit_people'] = $people;
		return $rarray;
	}
	
	protected function islogin(){
		if(empty($_COOKIE['user_userinfo'])){
			header("Location:http://e.hrtv.cn/ehrtv/Login/index?url=".$this->get_url());
		}else{
			return $_COOKIE['user_userinfo'];
		}
	}

}
?>